from flask import Flask, render_template, request, escape
import geo
import driving
from panduan import panduan_login
import weather
from log_sys import log_request
from pymysql import connect
app = Flask(__name__)



@app.route('/',methods=['GET','POST'])
def home():
    title = '欢迎来到驾车百宝箱~'
    return render_template("home.html",
                           the_title=title,)

@app.route('/login')
def login0() -> 'html':
    return render_template('login.html')

@app.route('/choice',methods=['POST'])
def choice() -> 'html':
  zhanghao = request.form.get("zhanghao")
  mima = request.form.get("mima")
  a = panduan_login(zhanghao, mima)
  if a == "true":
    return render_template('choice.html',the_title ="登录成功！")
  else:
    return "登录失败"	



@app.route('/regist')
def regist() -> 'html':
    return render_template('regist.html')


@app.route('/regist1',methods=["POST"])
def regist1():
  con = connect(host="127.0.0.1",port=3306,user="root",password="123456",db="user")
  cursor2 = con.cursor()
  zhanghao = request.form.get("zhanghao")
  mima = request.form.get("mima")
  sql = "insert into useradmin set uer_name='%s',password='%s'"% (zhanghao,mima)
  cursor2.execute(sql)
  con.commit()
  return render_template("regist1.html")

@app.route('/entry',methods=['POST'])
def entry01() -> 'html':
    return render_template('entry.html')


@app.route('/results', methods=['POST'])
def results01() -> 'html':
    key = request.form['key']
    start_address = request.form['start_address']
    end_address = request.form['end_address']
    title = '搜索结果'
    df = driving.driving01(key, start_address, end_address)
    results = df.loc[:, ['instruction', 'distance', 'duration', 'action']]
    results.columns = ['指引', '路程', '时间', '弯道']
    total_driving = driving.total_driving(df)
    total_driving_time = driving.total_driving_time(df)
    route = results['指引'].tolist()
    driving.log_request(request, route)
    return render_template('results.html',
                           the_title=title,
                           the_start_address=start_address,
                           the_end_address=end_address,
                           the_results=results,
                           the_html=results.to_html(index=False),
                           the_total_driving=total_driving,
                           the_total_driving_time=total_driving_time,)


@app.route('/log', methods=['POST'])
def log01() -> 'html':
    contents = []
    with open('driving.log', 'r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    titles = ('用户输入数据', '远程地址', '用户代理', '搜索结果')
    return render_template('log.html',
                           the_title='查看日志',
                           the_row_titles=titles,
                           the_data=contents, )

@app.route('/weather-search')
def entry_page() -> 'html':
    title = '欢迎来到天气占卜屋~'
    return render_template('weather-search.html',
                           the_title=title)



@app.route('/quanbu', methods=['POST'])
# 这些数据来自哪里？
def quanbu_search() -> 'html':
    key1 = "5ae4d72387a97e8529903ac948ab9eff"
    color1 = request.form['color1']
    color2 = request.form['color2']
    city = request.form['city']
    title = '这里是占卜的结果：'
    results = str(weather.quanbu(key1, city))
    log_request(request,results)

    return render_template('all.html',
                           the_title=title,
                           the_city=city,
                           the_weather=results,
                           the_results=results,
                           the_color1=color1,
                           the_color2=color2,)

@app.route('/viewlog',methods=['POST'])
def view_the_log() -> 'html':
    """display the contents of thr log file as a html table"""
    contents = []
    with open('weather.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理','用户结果')
    return render_template('viewlog.html',
                           the_title='日志系统后台',
                           the_row_title=titles,
                           the_data=contents)

if __name__ == '__main__':
    app.run()
